﻿using System;
using System.ComponentModel;
using System.Web.UI;
using DotNetNuke.Common;
using DotNetNuke.UI.UserControls;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Common.Utilities;

namespace HeroldIT.Dnn.Modules.WebControls
{
    /// <summary>
    /// A control for making a <see cref="UrlControl"/> control usable through
    /// the <see cref="ITextControl"/> interface.
    /// </summary>
    public class UrlControlWrapper : Control, ITextControl
    {
        #region properties

        private string urlControlID = "";

        /// <summary>
        /// Gets or sets the id of the referenced <see cref="UrlControl"/> control.
        /// </summary>
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [IDReferenceProperty(typeof(UrlControl))]
        [Localizable(false)]
        [DefaultValue("")]
        public string UrlControlID
        {
            get
            {
                return this.urlControlID;
            }
            set
            {
                if (null == value)
                    value = "";
                this.urlControlID = value;
                if (null != this.text && null != this.UrlControl)
                {
                    this.UrlControl.Url = this.text;
                }
            }
        }

        /// <summary>
        /// Gets the referenced <see cref="UrlControl"/> control or null if currently unavailable.
        /// </summary>
        protected UrlControl UrlControl
        {
            get
            {
                if (String.IsNullOrEmpty(this.UrlControlID))
                    return null;

                return this.NamingContainer.FindControl(this.UrlControlID) as UrlControl;
            }
        }

        private string text;

        /// <summary>
        /// Gets or sets the referenced <see cref="UrlControl"/> control's Url.
        /// </summary>
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (null != this.UrlControl)
                {
                    this.text = Globals.LinkClick(this.UrlControl.Url, this.PortalSettings.ActiveTab.TabID, Null.NullInteger, this.UrlControl.Track);
                }
                return this.text;
            }
            set
            {
                this.text = value;
                if (null != value && null != this.UrlControl)
                {
                    this.UrlControl.Url = value;
                }
            }
        }

        /// <summary>
        /// Gets the selected url.
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public string Url
        {
            get
            {
                if (null != this.UrlControl)
                {
                    return this.UrlControl.Url;
                }
                return this.Text;
            }
        }

        /// <summary>
        /// Gets whether the url should be opened in a new window.
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public bool OpenInNewWindow
        {
            get
            {
                if (null != this.UrlControl)
                {
                    return this.UrlControl.NewWindow;
                }
                return false;
            }
        }

        /// <summary>
        /// Gets whether the link will be tracked.
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public bool EnableTracking
        {
            get
            {
                if (null != this.UrlControl)
                {
                    return this.UrlControl.Track;
                }
                return false;
            }
        }

        /// <summary>
        /// Gets whether the link clicks will be logged.
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public bool LogActivity
        {
            get
            {
                if (null != this.UrlControl)
                {
                    return this.UrlControl.Log;
                }
                return false;
            }
        }

        /// <summary>
        /// Gets the selected url type.
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public string UrlType
        {
            get
            {
                if (null != this.UrlControl)
                {
                    return this.UrlControl.UrlType;
                }
                return "";
            }
        }

        private PortalSettings portalSettings;

        /// <summary>
        /// Gets the current <see cref="DotNetNuke.Entities.Portals.PortalSettings"/>.
        /// </summary>
        protected PortalSettings PortalSettings
        {
            get {
                if (null == this.portalSettings)
                {
                    this.portalSettings = PortalController.GetCurrentPortalSettings();
                }
                return this.portalSettings;
            }
        }

        #endregion

        #region page lifecycle

        /// <summary>
        /// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
        }

        #endregion
    }
}
